﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShanNiang.Mvvm.ViewModels
{
    /// <summary>
    /// 箱码对象
    /// </summary>
    public partial class BoxViewModel : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="boxCode"></param>
        public BoxViewModel(int boxSize=0)
        {
            BottleCodes = new ObservableCollection<BottleCodeViewModel>();
            for (int i = 0; i < boxSize; i++)
            {
                BottleCodes.Add(new BottleCodeViewModel());
            }
        }
        /// <summary>
        /// 箱码
        /// </summary>
        [ObservableProperty]
        private string? _boxCode;
        /// <summary>
        /// 瓶码集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<BottleCodeViewModel> _bottleCodes;
        /// <summary>
        /// 瓶码数量
        /// </summary>
        [ObservableProperty]
        private int _bottleCount;
        /// <summary>
        /// true 表示修改
        /// </summary>
        [ObservableProperty]
        private bool _modification;
        /// <summary>
        /// 是否装满
        /// </summary>
        [ObservableProperty]
        private bool _isFull;
        /// <summary>
        /// 箱码是否有效
        /// </summary>
        public bool Valid => !string.IsNullOrEmpty(BoxCode);

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(BoxCode))
            {
                OnPropertyChanged(nameof(Valid));
            }
            base.OnPropertyChanged(e);
        }
        /// <summary>
        /// 已装箱瓶数
        /// </summary>
        public int PackedCount => BottleCodes.Where(p => p.Valid).Count();

        public void NotifyPackedCountToChange()
        {
            OnPropertyChanged(nameof(PackedCount));
        }

    }
}
